
/***
 * @Description:本文件主要负责的是协议栈功能的测试(包括网络数据包测试，各种协议代码的测试)
 * @Author: Liverpool
 * @Date: 2024-03-24 19:00:07
 * @LastEditTime: 2024-03-24 19:06:32
 * @LastEditors: Liverpool
 */
#include "feature_text.h"
#include "tools.h"

/***
 * @description: 对于网络数据包功能的测试函数
 * @Author: Liverpool
 * @Date: 2024-03-24 19:07:06
 * @return {*}
 */
void packet_text(void)
{
    D_Packet *packet = packet_alloc(2000);
    for (int i = 0; i < 16; i++)
    {
        packet_add_header(packet, 33, Packet_Header_Continuous);
    }
    for (int i = 0; i < 16; i++)
    {
        packet_remove_header(packet, 33);
    }
    for (int i = 0; i < 16; i++)
    {
        packet_add_header(packet, 33, Packet_Header_NoContinuous);
    }
    for (int i = 0; i < 16; i++)
    {
        packet_remove_header(packet, 33);
    }
    packet_free(packet);
    packet = packet_alloc(8);
    packet_resize(packet, 32);
    packet_resize(packet, 288);
    packet_resize(packet, 4922);
    packet_resize(packet, 1921);
    packet_resize(packet, 288);
    packet_resize(packet, 32);
    packet_resize(packet, 0);
    packet_free(packet);

    packet = packet_alloc(689);
    D_Packet *src_packet = packet_alloc(892);
    packet_join(packet, src_packet);
    packet_free(packet);
    // 32-4-16-54-32-28
    packet = packet_alloc(32);
    packet_join(packet, packet_alloc(4));
    packet_join(packet, packet_alloc(16));
    packet_join(packet, packet_alloc(54));
    packet_join(packet, packet_alloc(32));
    packet_join(packet, packet_alloc(38));
    packet_set_continuous(packet, 44);
    packet_set_continuous(packet, 60);
    packet_set_continuous(packet, 64);
    packet_set_continuous(packet, 128);
    packet_set_continuous(packet, 135);
    packet_free(packet);

    packet = packet_alloc(32);
    packet_join(packet, packet_alloc(4));
    packet_join(packet, packet_alloc(16));
    packet_join(packet, packet_alloc(54));
    packet_join(packet, packet_alloc(32));
    packet_join(packet, packet_alloc(38));
    packet_join(packet, packet_alloc(512));
    packet_join(packet, packet_alloc(1000));
    packet_reset_access(packet);
    static uint16_t temp[1000];
    for (int i = 0; i < 1000; i++)
    {
        temp[i] = i;
    }
    packet_write(packet, (uint8_t *)temp, packet_total_size(packet));
    static uint16_t read_temp[1000];
    plat_memset(read_temp, 0, sizeof(uint16_t) * 1000);
    packet_reset_access(packet);
    packet_read(packet, (uint8_t *)read_temp, packet_total_size(packet));
    if (plat_memcmp(temp, read_temp, packet_total_size(packet)) != 0)
    {
        plat_printf("not equal\n");
        return;
    }
    plat_memset(read_temp, 0, sizeof(uint16_t) * 1000);
    packet_seek(packet, 18 * 2);
    packet_read(packet, (uint8_t *)read_temp, 56);
    if (plat_memcmp(temp + 18, read_temp, 56) != 0)
    {
        plat_printf("not equal\n");
        return;
    }
    plat_memset(read_temp, 0, sizeof(uint16_t) * 1000);
    packet_seek(packet, 85 * 2);
    packet_read(packet, (uint8_t *)read_temp, 256);
    if (plat_memcmp(temp + 85, read_temp, 256) != 0)
    {
        plat_printf("not equal\n");
        return;
    }
    D_Packet *dest = packet_alloc(1024);
    packet_seek(dest, 600);
    packet_seek(packet, 200);
    packet_copy(dest, packet, 122);
    plat_memset(read_temp, 0, sizeof(uint16_t) * 1000);
    packet_seek(dest, 600);
    packet_read(dest, (uint8_t *)read_temp, 122);
    if (plat_memcmp(temp + 100, read_temp, 122) != 0)
    {
        plat_printf("not equal\n");
        return;
    }
    packet_seek(dest, 0);
    packet_fill(dest, 78, packet_total_size(dest));
    plat_memset(read_temp, 0, sizeof(uint16_t) * 1000);
    packet_seek(dest, 0);
    packet_read(dest, (uint8_t *)read_temp, packet_total_size(dest));
    uint8_t *ptr = (uint8_t *)read_temp;
    for (int i = 0; i < packet_total_size(dest); i++)
    {
        if ((*ptr++) != 78)
        {
            plat_printf("not equal\n");
            return;
        }
    }

    packet_free(dest);
    packet_free(packet);
}

/***
 * @description: 网络接口相关功能测试
 * @Author: Liverpool
 * @Date: 2024-04-25 01:29:18
 * @return {*}
 */
void net_interface_text(void)
{
    // D_Net_Interface *netif = net_interface_open("pcap");
}
/***
 * @description: 以太网相关功能测试
 * @Author: Liverpool
 * @Date: 2024-05-19 19:59:54
 * @return {*}
 */
void Ethernet_text(void)
{
    // 大小端转化
    uint32_t v1 = x_ntohl(0x12345678);
    uint16_t v2 = x_ntohs(0x1234);
}
/***
 * @description: 负责对外功能测试的外部函数接口
 * @Author: Liverpool
 * @Date: 2024-03-24 19:04:19
 * @return {*}
 */
void feature_text(void)
{
    // packet_text();
    net_interface_text();
    Ethernet_text();
}